package algorithms.que11;

import com.fasterxml.jackson.databind.BeanProperty;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

/**
 * @功能
 * @创建时间 2020/6/7
 * 画图不理解
 */
public class que32 {

    public static double[] segmentation(int N,double l,double r,double[] a){
        if(N==0) return a;
        double s=(r-1)/N;
        a[0]=1;
        for(int i=1;i<a.length;i++){
            a[i]=a[i-1]+s;
        }
        return a;
    }
    public static void makehistogram(double[] a,double[] b,double l,double r){
        int[] c =new int[a.length-1];
        for(int i=0;i<b.length;i++)
            for(int j=0;j<a.length-1;j++){
                if(b[i]>=a[j]&&b[i]<a[j+1]){
                    c[j]++;
                    continue;
                }
            }
        int N=c.length;
        StdDraw.setXscale(0,(r-1)*1.2);
        StdDraw.setYscale(0,b.length/N*1.5);
        for (int i=0;i<N;i++){
            double x=1+(r-1)/N*i;
            double y = c[i]/2.0;
            double rw = (r-1)/(2*N);
            double rh = c[i]/2.0;
            StdDraw.filledRectangle(x,y,rw,rh);
            StdOut.print(c[i]+"");
        }
    }

    public static void main(String[] args) {
        int N=10;
        double l=2;
        double r =20;
        double[] a = new double[N+1];
        double[] b = new double[N*N*N];
        a=segmentation(N,l,r,a);
        for(int i=0;i<b.length;i++){
            b[i] = StdRandom.uniform(l,r);
        }
        makehistogram(a,b,l,r);
    }
}
